package cn.com.dhc.danlu.widget.home;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.widget.RotateImageView;

/**
 * @author: 李一方
 * @time: 2016-08-04 10:42
 * @mail: liyifang@danlu.com
 * @desc: 首页上显示的ListView需要用到的HeaderView  丹露促销、丹露推荐等
 */
public class HeaderView extends FrameLayout {
    private ImageView ivReplace;

    private TextView tvRefresh;
    private RotateImageView rivRefresh;

    private LinearLayout llRefresh;

    private OnRefreshViewCickListener onRefreshViewCickListener;

    private boolean isRotate = false;

    public HeaderView(Context context) {
        super(context, null);
        init();
    }

    public HeaderView(Context context, AttributeSet attrs) {
        super(context, attrs);

        init();
    }

    public void setOnRefreshViewCickListener(OnRefreshViewCickListener onRefreshViewCickListener) {
        this.onRefreshViewCickListener = onRefreshViewCickListener;
    }

    private void init() {
        View rootView = LayoutInflater.from(getContext()).inflate(R.layout.dl_header_home, this, true);
        ivReplace = (ImageView) rootView.findViewById(R.id.iv_relace);
        tvRefresh = (TextView) rootView.findViewById(R.id.tv_refresh);
        rivRefresh = (RotateImageView) rootView.findViewById(R.id.riv_refresh);
        llRefresh = (LinearLayout) rootView.findViewById(R.id.ll_refresh);
        llRefresh.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isRotate) return;
                startRefresh();
                if (null != onRefreshViewCickListener) {
                    onRefreshViewCickListener.onClick(v);
                }
            }
        });
        setBackgroundColor(getResources().getColor(R.color.white));
    }

    public void isShowRefresh(boolean isShow){
        llRefresh.setVisibility(isShow ? VISIBLE : GONE);
    }

    public void startRefresh() {
        isRotate = true;
        rivRefresh.start(RotateImageView.ANTI_CLOCKWISE);
        tvRefresh.setText(getResources().getString(R.string.data_is_loading));
    }

    public void stopRefresh() {
        isRotate = false;
        rivRefresh.stop();
        tvRefresh.setText(getResources().getString(R.string.change_set));
    }
    /**
     * 设置header上需要替换的图片资源
     * @param resId 资源ID
     */
    public void setIvReplace(int resId) {
        ivReplace.setImageResource(resId);
    }
    public interface OnRefreshViewCickListener {
        void onClick(View v);
    }
}